<?php
namespace App\Repositories\Admin;

use App\Models\Admin\OrderModel;
use App\Repositories\BaseRepository;

class OrderRepository extends BaseRepository
{
    protected $repository;

    public function __construct()
    {
        $this->repository = new OrderModel();
    }

    /**
     * 操作分页获取
     */
    public function getIndexList()
    {
        $models = $this->repository->orderBy('id','asc')
            ->paginate(10);
        $models->limit = 10;
        return $models;
    }

    /**
     * 本公司订单
     */
    public function getIndexByCid($uid)
    {
        return $this->repository->where('cid',$uid)
            ->orderBy('id','asc')
            ->skip(0)
            ->take(10)
            ->get();
    }

    /**
     * 本工厂加工的
     */
    public function getIndexByFid($uid)
    {
        return $this->repository->where('fid',$uid)
            ->orderBy('id','asc')
            ->skip(0)
            ->take(10)
            ->get();
    }

    public function setAdd($data)
    {
        $data['order_no'] = date('YmdHis',time()).rand(1000,9999);
        $data['created_at'] = time();
        return $this->repository->insertGetId($data);
    }

    public function setModify($data,$id)
    {
        $data['updated_at'] = time();
        return $this->repository->where('id',$id)
            ->update($data);
    }

    /**
     * 确认验收报告
     */
    public function setStatusSure($orderId)
    {
        return $this->repository->where('id',$orderId)
            ->where('status',3)
            ->update(array('status'=>4));
    }

    public function getShowOne($id)
    {
        return $this->repository->where('id',$id)->first();
    }

    /**
     * 抢单状态
     */
    public function geStatusList()
    {
        return $this->repository->getStatuss();
    }

    /**
     * 抢单类型名称
     */
    public function getStatusName($status)
    {
        $statuss = $this->geStatusList();
        return array_key_exists($status,$statuss) ? $statuss[$status] : '';
    }

    /**
     * 工厂，生产状态，设置
     */
    public function setOrderMakeStatus($orderId)
    {
        return $this->repository->where('id',$orderId)
            ->where('status',1)
            ->update(array('status'=>2));
    }
}